package com.google.code.jholidays.core;

import com.google.code.jholidays.events.IEvent;
import com.google.code.jholidays.io.IDescriptorReader;
import com.google.code.jholidays.io.csv.CsvReader;
import com.google.code.jholidays.io.jdbc.JdbcReader;
import com.google.code.jholidays.io.xml.XmlReader;

/**
 * Represents descriptor which is used to create {@link IEvent} instances.
 * <p>
 * Normally you mustn't create {@link EventDescriptor} manually.
 * {@link DescriptorCollection} should be created instead using concrete
 * implementation of {@link IDescriptorReader} interface. This collection then
 * can be passed to {@link EventBuilder} in order to create events.
 * <p>
 * <b>Example usage:</b> <code><pre>
 * 	IDescriptorReader reader = new XmlReader("input.xml");
	DescriptorCollection coll = reader.read();
	
	System.out.println(coll.size());

	EventBuilder builder = new EventBuilder();
	List<IEvent> events = builder.buildEvents(coll);

	final int year = 2009;

	for (IEvent event : events) {
	    System.out.println(event.getDate(year));
	}
 * </pre></code> In this example we first use {@link XmlReader} implementation
 * in order to read descriptors collection from XML file (see {@link XmlReader}
 * for details). Then we pass this collection to {@link EventBuilder} which
 * processes descriptors from collection and creates {@link IEvent} instances.
 * Finally we print date of each event. Here is output of code above:
 * <code><pre>
 * 	3
	Sat Jan 10 00:00:00 MSK 2009
	Wed Sep 30 00:00:00 MSD 2009
	Sat Oct 03 00:00:00 MSD 2009
 * </pre></code> There are several implementations of {@link IDescriptorReader}
 * interface: {@link CsvReader}, {@link XmlReader}, {@link JdbcReader}.
 * 
 * @see DescriptorCollection
 * @see IDescriptorReader
 * @see EventBuilder
 * @author tillias
 * 
 */
public class EventDescriptor {

    public static final int ROOT_ID = 0;

    /**
     * Creates new instance of descriptor.
     * 
     * @param id
     *            Event id
     */
    public EventDescriptor(int id) {
	this.id = id;
	this.parentID = ROOT_ID;
    }

    /**
     * Creates new instance of descriptor.
     * 
     * @param id
     *            Event id
     * @param parentID
     *            Parent event id
     */
    public EventDescriptor(int id, int parentID) {
	this.id = id;
	this.parentID = parentID;
    }

    /**
     * Gets event name
     * 
     * @return Event name
     */
    public String getName() {
	return name;
    }

    /**
     * Sets event name
     * 
     * @param name
     *            New event name
     */
    public void setName(String name) {
	this.name = name;
    }

    /**
     * Gets event textual description
     * 
     * @return Event description
     */
    public String getDescription() {
	return description;
    }

    /**
     * Sets event textual description
     * 
     * @param description
     *            New event description
     */
    public void setDescription(String description) {
	this.description = description;
    }

    /**
     * Gets event expression
     * 
     * @return Event expression
     */
    public String getExpression() {
	return expression;
    }

    /**
     * Sets event expression
     * 
     * @param expression
     *            New event expression
     */
    public void setExpression(String expression) {
	this.expression = expression;
    }

    /**
     * Gets event id
     * 
     * @return Event id
     */
    public int getID() {
	return id;
    }

    /**
     * Gets parent event id
     * 
     * @return Parent event id
     */
    public int getParentID() {
	return parentID;
    }

    /**
     * Gets value indicating that {@link IEvent} that might be constructed using
     * this descriptor <b>is root</b> e.g. has no parent event
     * 
     * @return True if event is root, false otherwise
     */
    public boolean isRoot() {
	return parentID == ROOT_ID;
    }

    @Override
    public String toString() {
	return id + ". " + name + " " + description + " " + parentID + " "
		+ expression;
    }

    int id;
    int parentID;
    String name;
    String description;
    String expression;

}
